﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace Hired
{
    public partial class imgUpload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            string imagePathName = "";
            switch(DBAccount.RetrieveAccountTypeByID(User.Identity.Name)){
                case "JobseekerAccount":
                    JobseekerAccount js = DBJobseekerAccount.retrieveJobseekerAccount(User.Identity.Name, false);
                    imagePathName = "/Users/" + js.JAccountID + "/";
                    break;
                case"EmployerPassport":
                    EmployerPassport ep = DBEmployerPassport.RetrieveEmployerPassport(User.Identity.Name);
                    imagePathName = "/Users/" + ep.EPassportID + "/";
                    break;
                case"CompanyAccount":
                    CompanyAccount ca = DBCompanyAccount.RetrieveCompanyAccount(User.Identity.Name, false);
                    imagePathName = "/Users/" + ca.CAccountID + "/";
                    break;
            }
            System.IO.Directory.CreateDirectory(imagePathName);
            if (fileUploadImage.HasFile)
            {
                string fileservername = fileUploadImage.FileName;
                imagePathName += fileservername;
                HttpPostedFile f = fileUploadImage.PostedFile;
                if (checkFileExt(f))
                {
                    fileUploadImage.SaveAs(imagePathName);
                    switch (DBAccount.RetrieveAccountTypeByID(User.Identity.Name)) {
                        case "JobseekerAccount":
                            JobseekerAccount js = DBJobseekerAccount.retrieveJobseekerAccount(User.Identity.Name, false);
                            js.Jobseeker.ImageAddress = imagePathName;
                            break;
                        case "EmployerPassport":
                            EmployerPassport ep = DBEmployerPassport.RetrieveEmployerPassport(User.Identity.Name);
                            ep.CompanyAccount.Company.ImageAddress = imagePathName;
                            break;
                        case "CompanyAccount":
                            CompanyAccount ca = DBCompanyAccount.RetrieveCompanyAccount(User.Identity.Name, false);
                            ca.Company.ImageAddress = imagePathName;
                            break;
                    }
                }
            }
        }
        public Boolean checkFileExt(HttpPostedFile fileUpload)
        {
            if (fileUpload != null && fileUpload.ContentLength > 0)
            {
                FileInfo finfo = new FileInfo(fileUpload.FileName);
                string fileExtension = finfo.Extension.ToLower();
                if (fileExtension == ".png" || fileExtension == ".jpg" || fileExtension == ".gif" || fileExtension == ".bmp" || fileExtension ==".tif")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
    }
}